package br.com.felix.fwt.ui;

import java.io.PrintWriter;

import org.apache.log4j.Logger;

import br.com.felix.fwt.log.LoggerFactory;
import br.com.felix.fwt.ui.exception.ComponentRenderException;

public class RadioButton extends Input {

	private static final long serialVersionUID = -6549531777615416958L;

	private static Logger logger = LoggerFactory.getLogger(RadioButton.class);
	
	private RadioGroup radioGroup;
	
	private String label;
	
	private boolean checked = false;
	
	public RadioButton(){
	}
	
	public RadioButton(String label, String value){
	    super(value);
		this.label = label;
	}
	
	{
		this.cssClass = "fwtradiobutton";
	}
	
	
	@Override
	public void write(PrintWriter out) throws ComponentRenderException {
		if(!authorized()) return;
		PresentationMode pMode = getPresentationMode();
		switch(pMode){
			case EDIT:
				out.write("\n<input type=\"radio\"");
				out.write(" name=\"");
				if(radioGroup!=null){
					out.write(radioGroup.getName());
				}
				else{
					logger.warn("RadioButton without a name");
				}
				out.write("\" value=\"");
				String value = getValue();
				if(value!=null)out.write(value);
				out.write("\"");
				if(checked)out.write(" checked=\"true\" ");
				if(style!=null)out.write(style);
				out.write(" />");
				if(label!=null){
					out.write(label);
				}
				break;
			case SHOW:
				if(label!=null){
					out.write(label);
				}
				break;
			case DELETED:
				if(label!=null){
					out.write("\n<del>");
					out.write(label);
					out.write("</del>");
				}
		}
		
		
	}


	@Override
	public void setValue(String newValue) {
		System.out.println("RadioButton set value -----------------");
		radioGroup.setValue(newValue);
	}


	public String getLabel() {
		return label;
	}


	public void setLabel(String label) {
		this.label = label;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + ((label == null) ? 0 : label.hashCode());
		result = prime * result + ((radioGroup == null) ? 0 : radioGroup.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		RadioButton other = (RadioButton) obj;
		if (label == null) {
			if (other.label != null)
				return false;
		}
		else
			if (!label.equals(other.label))
				return false;
		if (radioGroup == null) {
			if (other.radioGroup != null)
				return false;
		}
		else
			if (!radioGroup.equals(other.radioGroup))
				return false;
		return true;
	}

	public RadioGroup getRadioGroup() {
		return radioGroup;
	}

	public void setRadioGroup(RadioGroup radioGroup) {
		this.radioGroup = radioGroup;
	}

	public void setChecked(boolean checked) {
		this.checked = checked;
	}

	public boolean isChecked() {
		return checked;
	}

}
